package com.xiaoming.freshmenapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.xiaoming.freshmenapp.R;
import com.xiaoming.freshmenapp.bean.ClassmateBean;

import org.json.JSONObject;

import java.util.List;

public class ClassmateListActivity extends AppCompatActivity {

    private ImageView home_back;
    private TextView home_title;
    private LinearLayout home_layout;
    private ListView cm_lv_listview;
    private List<ClassmateBean.ListBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classmate_list);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {

        cm_lv_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ClassmateBean.ListBean listBean = list.get(position);
                Intent intent = new Intent();
                Toast.makeText(ClassmateListActivity.this, "选择了"+listBean.getName(), Toast.LENGTH_SHORT).show();
                intent.setClass(getApplicationContext(),ClassmateDetailActivity.class);
                intent.putExtra("id",listBean.getId());
                startActivity(intent);
            }
        });
    }

    private void initData() {

        getData();

    }

    private void getData() {

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://10.2.64.83:8080/app3/classmate/list";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: " + jsonObject.toString());
                Gson gson = new Gson();
                ClassmateBean data = gson.fromJson(jsonObject.toString(), ClassmateBean.class);
                list = data.getList();
                cm_lv_listview.setAdapter(new ClassmateAdapter());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误" + volleyError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);

    }

    private void initView() {
        home_back = (ImageView) findViewById(R.id.home_back);
        home_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        home_title = (TextView) findViewById(R.id.home_title);
        home_title.setText("同学列表");
        home_layout = (LinearLayout) findViewById(R.id.home_layout);
        home_layout.setBackground(getResources().getDrawable(R.color.colorAccent));
        cm_lv_listview = (ListView) findViewById(R.id.cm_lv_listview);

    }

    public class ClassmateAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (holder==null){
                convertView = View.inflate(getApplicationContext(), R.layout.classmate_item_layout, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            ClassmateBean.ListBean listBean = list.get(position);

            holder.item_tv_name.setText(listBean.getName());

            return convertView;
        }

        public class ViewHolder {
            public View rootView;
            public ImageView item_tv_icon;
            public TextView item_tv_name;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.item_tv_icon = (ImageView) rootView.findViewById(R.id.item_tv_icon);
                this.item_tv_name = (TextView) rootView.findViewById(R.id.item_tv_name);
            }

        }
    }
}